package com.ruoyi.web.controller.imageResource;


import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.fastDfs.IFastDfsService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;

@RestController
@RequestMapping("/fastDfs")
public class FastDfsController {

    @Resource
    private IFastDfsService fastDfsService;

    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile file){
        try {
            String originalFilename = file.getOriginalFilename();
            String extension = FilenameUtils.getExtension(originalFilename);
            String path= fastDfsService.upload(file.getBytes(),extension);
            return AjaxResult.success(path);
        }catch (Exception e){
            return AjaxResult.error("上传文件失败");
        }
    }


    @DeleteMapping("/delete")
    public AjaxResult delete(String path){
        try {
            fastDfsService.delete(path);
            return AjaxResult.success();
        }catch (Exception e){
            return AjaxResult.error("删除文件失败");
        }
    }

}